package com.fkframe.codegener.ui.view;

import heip.generator.entity.PropEditor;
import heip.generator.entity.PropEditor.EditType;
import heip.generator.entity.ValidateRule;
import heip.generator.entity.ValidateRule.ValidateType;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.LinkedHashMap;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public abstract class CommonView extends JPanel implements ActionListener {

	public JTable renderColumnEditTable() {
		SimplePropTableModel tableModel = new SimplePropTableModel();
		// 声明表格模型
		LinkedHashMap<String, String> columnMap = new LinkedHashMap<String, String>();
		// columnMap.put("seq", "序号");
		columnMap.put("name", "属性");
		columnMap.put("cnName", "显示名");

		columnMap.put("isQuery", "是否查询");
		columnMap.put("isTable", "列表显示");
		columnMap.put("isEdit", "是否编辑");

		columnMap.put("editType", "编辑类型");
		// columnMap.put("editRule", "规则");

		columnMap.put("validateType1", "是否必输");
		/*
		 * columnMap.put("validateRule1", "校验规则1");
		 * columnMap.put("validateType2", "校验类型2");
		 * columnMap.put("validateRule2", "校验规则2");
		 */
		// columnMap.put("validateType3", "校验类型3");
		// columnMap.put("validateRule3", "校验规则3");

		tableModel.setColumnMap(columnMap);

		JTable table = new JTable(tableModel);
		table.setAutoscrolls(true);
		table.setFocusable(false);
		table.setColumnSelectionAllowed(false);
		table.setRowHeight(30);
		table.setAutoResizeMode(0);

		SimplePropTableModel model = (SimplePropTableModel) table.getModel();
		TableColumn sportColumn = table.getColumn(model
				.getColumnNameByKey("validateType1"));
		sportColumn.setWidth(50);
		// TableColumn sportColumn2 =
		// this.table.getColumn(model.getColumnNameByKey("validateType2"));
		// TableColumn sportColumn3 =
		// this.table.getColumn(model.getColumnNameByKey("validateType3"));
		setUpSportColumn(sportColumn);
		// setUpSportColumn(sportColumn2);

		TableColumn sportColumn3 = table.getColumn(model
				.getColumnNameByKey("editType"));
		setEditTypeColumn(sportColumn3);

		return table;
	}

	public void setEditTypeColumn(TableColumn column) {
		JComboBox comboBox = new JComboBox();
		comboBox.addItem("");
		for (EditType type : EditType.values()) {
			comboBox.addItem(type.getName());
		}
		column.setCellEditor(new DefaultCellEditor(comboBox));

		// Set up tool tips for the sport cells.
		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
		// renderer.setToolTipText("Click for combo box");
		column.setCellRenderer(renderer);
	}

	public void setUpSportColumn(TableColumn sportColumn) {

		// SimplePropTableModel model = (SimplePropTableModel) table.getModel();
		// TableColumn sportColumn =
		// this.table.getColumn(model.getColumnNameByKey("validateType1"));

		JComboBox comboBox = new JComboBox();

		comboBox.addItem("");
		comboBox.addItem(ValidateType.REQUIRED.getName());

		/*
		 * for(ValidateType type : ValidateType.values()){
		 * comboBox.addItem(type.getName()); }
		 */
		sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

		// Set up tool tips for the sport cells.
		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
		// renderer.setToolTipText("Click for combo box");
		sportColumn.setCellRenderer(renderer);

	}

	public PropEditor buildEditor(String typeStr, String param) {
		PropEditor editor = null;

		EditType edittype = null;
		for (EditType type : EditType.values()) {
			if (typeStr.equals(type.getName())) {
				edittype = type;
				break;
			}
		}

		if (edittype == null) {
			edittype = EditType.TEXTINPUT;
		}

		editor = new PropEditor(edittype);
		/*
		 * switch (edittype) { case SELECT: case DIALOG: case TEXTINPUT: editor
		 * = new PropEditor(edittype); break; default: break; }
		 */

		return editor;

	}

	public ValidateRule buildValidateRule(String typeStr, String ruleStr) {

		ValidateRule rule = null;

		ValidateType validatetype = null;
		for (ValidateType type : ValidateType.values()) {
			if (typeStr.equals(type.getName())) {
				validatetype = type;
				break;
			}
		}
		if (validatetype == null)
			return null;

		switch (validatetype) {
		case REQUIRED:// 必输校验
			rule = new ValidateRule(ValidateType.REQUIRED);
			return rule;
		case LENGTH:// 长度校验
			rule = new ValidateRule(ValidateType.LENGTH);
			String[] sizes = ruleStr.split(",");
			if (sizes.length > 1) {
				rule.setMinSize(sizes[0]);
				rule.setMaxSize(sizes[1]);
			} else {
				rule.setMaxSize(sizes[0]);
			}
			break;
		case INTEGER:// 整数校验
			rule = new ValidateRule(ValidateType.INTEGER);
			String[] ss = ruleStr.split(",");
			if (ss.length > 1) {
				rule.setMin(ss[0]);
				rule.setMax(ss[1]);
			} else {
				rule.setMax(ss[0]);
			}
			break;
		case DECIMAL:
			String[] sss = ruleStr.split(",");
			rule = new ValidateRule(ValidateType.DECIMAL);
			if (sss.length > 1) {
				rule.setMin(sss[0]);
				rule.setMax(sss[1]);
			} else {
				rule.setMax(sss[0]);
			}
			break;
		default:
			break;
		}

		return rule;
	}

	class ComboBoxCellRenderer implements TableCellRenderer {
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			JButton cmb = (JButton) value;
			if (isSelected) {
				cmb.setForeground(table.getSelectionForeground());
				cmb.setBackground(table.getSelectionBackground());
			} else {
				cmb.setForeground((unselectedForeground != null) ? unselectedForeground
						: table.getForeground());
				cmb.setBackground((unselectedBackground != null) ? unselectedBackground
						: table.getBackground());
			}
			cmb.setFont(table.getFont());
			if (hasFocus) {
				cmb.setBorder(UIManager
						.getBorder("Table.focusCellHighlightBorder"));
				if (!isSelected && table.isCellEditable(row, column)) {
					Color col;
					col = UIManager.getColor("Table.focusCellForeground");
					if (col != null) {
						cmb.setForeground(col);
					}
					col = UIManager.getColor("Table.focusCellBackground");
					if (col != null) {
						cmb.setBackground(col);
					}
				}
			} else {
				cmb.setBorder(noFocusBorder);
			}
			//cmb.addActionListener("");
			return cmb;
		}

		protected  Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
		private Color unselectedForeground;
		private Color unselectedBackground;
	}
}
